﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;

namespace ImageUtil
{
    [ComVisible(true)]
    [Guid("F3FEAF3A-6142-48B8-A9A3-3C345E9F7033")]
    [ProgId("ImageUtil.Base64ImageUtil")]
    public class Base64ImageUtil
    {
        public string GetBase64ImageData(string filePath)
        {
            if (File.Exists(filePath))
            {
                Image image = Image.FromFile(filePath);
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, image.RawFormat);
                    byte[] byteArray = ms.ToArray();
                    ms.Close();
                    return JsonConvert.SerializeObject(new
                    {
                        code = 0,
                        msg = "转换成功",
                        data = Convert.ToBase64String(byteArray)
                    });
                }
            }
            else
            {
                return JsonConvert.SerializeObject(new { code = 1, msg = "未找到对应的文件" });
            }

        }
    }
}
